package database;

import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import auxilary.Constants;
import auxilary.Log;

import entities.Course;
import entities.Session;
import entities.User;

public class DBDefault {
  public static void fillDatabase() {
    UserHandler uh = new UserHandler();
    //default users
    List<User> users = new LinkedList<User>();
    users.add(new User(-1,"admin","admin","admin","admin","admin@gmail.com","Physics",7,"052-7686878",true));
    users.add(new User(-1,"1","nayish","Nachshon","Schwartz","nayish@gmail.com","Computer Science",9,"09-8657843",false));
    users.add(new User(-1,"1","tomer","Tomer","Levinson","tomer@gmail.com","Computer Science",8,"07-4543211",false));
    for (int i=1; i<100; i++) {
    	String si = String.valueOf(i);
    	users.add(new User(-1,"1","user"+si,"User"+si,"Userberg","user"+si+"@gmail.com","Computer Science",8,"02-333444"+si,false));
    }
    for (User user : users) {
      try {
        user.setId(uh.addUser(user));
      }
      catch (Exception e){
    	  Log.dbg("Problem adding user " + user.getUsername());
    	  return;
      }
    }
    int adminId = 1;
    Log.dbg("Default users added");
    
    
    // default courses
    CourseHandler ch = new CourseHandler();
    List<Course> courses = new LinkedList<Course>();
    // Food courses
    int group = Constants.Group.FOOD;
    List<Session> sessions = new LinkedList<Session>();
     sessions.add(new Session(-1, 0, 2));
     sessions.add(new Session(-1, 10, 12));
     sessions.add(new Session(-1, 16,20));
    courses.add(new Course(-1,"Grapes and Vines","Some Blob about the course",5,30,group,sessions,adminId));
     sessions = new LinkedList<Session>();
     sessions.add(new Session(-1, 4, 6));
     sessions.add(new Session(-1, 14, 16));
     sessions.add(new Session(-1, 20,24));
    courses.add(new Course(-1,"Butter Spreading 101","In this course we will learn the correct way to spread butter.",4,80,group,sessions,adminId));
     sessions = new LinkedList<Session>();
     sessions.add(new Session(-1, 6, 8));
     sessions.add(new Session(-1, 28,30));
    courses.add(new Course(-1,"Food tasting","Yum....",2,5,group,sessions,adminId));
     sessions = new LinkedList<Session>();
     sessions.add(new Session(-1, 8, 10));
     sessions.add(new Session(-1, 56,59));
    courses.add(new Course(-1,"Banana Spliting","Learn how to make the perfect banana, split.",4,80,group,sessions,adminId));
     sessions = new LinkedList<Session>();
     sessions.add(new Session(-1, 50,52));
    courses.add(new Course(-1,"Marshmallow Roasting","Get your skewers ready.",3,20,group,sessions,adminId));


    // Household courses
    group = Constants.Group.HOUSEHOLD;
     sessions = new LinkedList<Session>();
     sessions.add(new Session(-1, 0, 2));
     sessions.add(new Session(-1, 10, 12));
     sessions.add(new Session(-1, 16,20));
    courses.add(new Course(-1,"Cat sorting","Here we will learn how to sort cats by color. Prerequisite: Color blindness test",5,100,group,sessions,adminId));
     sessions = new LinkedList<Session>();
     sessions.add(new Session(-1, 12, 16));
     sessions.add(new Session(-1, 2, 5));
    courses.add(new Course(-1,"House building","From monopoly houses to Dog houses to the t.v. show dr.",2,15,group,sessions,adminId));
     sessions = new LinkedList<Session>();
     sessions.add(new Session(-1, 43, 45));
    courses.add(new Course(-1,"Lightbulb Changing","How many people does it take to change a lightbulb? hopefully this courses capacity will do.",4,120,group,sessions,adminId));
     sessions = new LinkedList<Session>();
     sessions.add(new Session(-1, 20,23	));
    courses.add(new Course(-1,"Baby Sitting","Here you will learn how to sit on babies.",3,50,group,sessions,adminId));

    // magic courses
    group = Constants.Group.MAGIC;
     sessions = new LinkedList<Session>();
     sessions.add(new Session(-1, 19, 20));
    courses.add(new Course(-1,"Transfiguration","The transfiguration of objects and people.Teacher: Professor Minerve McGonogall",3,55,group,sessions,adminId));
     sessions = new LinkedList<Session>();
     sessions.add(new Session(-1, 22, 25));
     sessions.add(new Session(-1, 53, 56));
    courses.add(new Course(-1,"Charms","Basic wand movement and spells. Teacher: Professor Flitwick",5,15,group,sessions,adminId));
     sessions = new LinkedList<Session>();
     sessions.add(new Session(-1, 52, 53));
     sessions.add(new Session(-1, 39, 40));
    courses.add(new Course(-1,"Herbology","Magical plants and herbs and how to grow them. Teacher: Professor Flitwick",10,2,group,sessions,adminId));
     sessions = new LinkedList<Session>();
     sessions.add(new Session(-1, 25, 27));
    courses.add(new Course(-1,"History Of Magic","History of the magical world. Teacher: Professor Binns",4,25,group,sessions,adminId));
    
    // Sport courses
    group = Constants.Group.SPORT;
     sessions = new LinkedList<Session>();
     sessions.add(new Session(-1, 22, 24));
     sessions.add(new Session(-1, 33, 37));
    courses.add(new Course(-1,"Sand Castles","Learning to build sand castles, right in your own back yard",4,30,group,sessions,adminId));
     sessions = new LinkedList<Session>();
     sessions.add(new Session(-1, 45, 47));
    courses.add(new Course(-1,"Tree hugging","Hug trees in many shapes and colors.",5,80,group,sessions,adminId));
     sessions = new LinkedList<Session>();
     sessions.add(new Session(-1, 10, 13));
     sessions.add(new Session(-1, 1, 2));
    courses.add(new Course(-1,"Horse Chasing","Find out how to stop that annoying horse from chasing you.",1,2,group,sessions,adminId));
     sessions = new LinkedList<Session>();
     sessions.add(new Session(-1, 14, 18));
     sessions.add(new Session(-1, 5, 9));
    courses.add(new Course(-1,"Quidditch","Flying and playing our favorite game",10,14,group,sessions,adminId));

    // Clothes courses
    group = Constants.Group.CLOTHES;
     sessions = new LinkedList<Session>();
     sessions.add(new Session(-1, 0, 4));
    courses.add(new Course(-1,"Geneology","The art of wearing jeans",4,60,group,sessions,adminId));
     sessions = new LinkedList<Session>();
     sessions.add(new Session(-1, 18, 20));
     sessions.add(new Session(-1, 21, 23));
    courses.add(new Course(-1,"Where do the socks go","We will try to find the answer to the age old question: 'where do the socks disapeer to whe we put them into the washing machine'.",3,90,group,sessions,adminId));
     sessions = new LinkedList<Session>();
     sessions.add(new Session(-1, 40, 44));
     sessions.add(new Session(-1, 54, 58));
     sessions.add(new Session(-1, 30, 34));
    courses.add(new Course(-1,"Pocketless Shorts","To buy or not to buy that is the question we will answer.",2,10,group,sessions,adminId));
     sessions = new LinkedList<Session>();
     sessions.add(new Session(-1, 28, 30));
    courses.add(new Course(-1,"Shoes or Sandals","Learning to read people's toes.",1,2,group,sessions,adminId));

    // Resting courses
    group = Constants.Group.RESTING;
     sessions = new LinkedList<Session>();
     sessions.add(new Session(-1, 60, 63));
    courses.add(new Course(-1,"Intro to Sleeping","ZzZzZz...",10,900,group,sessions,adminId));
     sessions = new LinkedList<Session>();
     sessions.add(new Session(-1, 63, 66));
    courses.add(new Course(-1,"Book reading","Read books and enjoy the day...",10,900,group,sessions,adminId));
     sessions = new LinkedList<Session>();
     sessions.add(new Session(-1, 66, 70));
    courses.add(new Course(-1,"Not Using Electricity","Find what can be done when you don't use electricity",10,900,group,sessions,adminId));

    
    for (Course course : courses) {
        try {
          ch.addCourse(course);
        }
        catch (Exception e){
        	  Log.dbg("Problem adding course " + course.getName());
        	  return;
        }
    }   
    if ((courses.size() != 0) && (users.size() != 0)) {
	    Log.dbg("Default courses added");
	    Random random = new Random(courses.size());
	    for (User user : users) {
	    	boolean flag = false;
	    	while (!flag) {
		    	int courseId = random.nextInt(courses.size())+1;
		    	System.out.println(courseId);
		        try {
		          ch.changeUserCourseStatus(courseId,user,1);
		        }
		        catch (Exception e){
		        	  flag = true;
		        }
	    	}
	    } 
	    Log.dbg("Random courses added to users");
    }
  }
  public static void resetDBToDefault() {
	    //empty the db
	    DBCreator dbc = new DBCreator();
	    dbc.deleteDatabase();
	    dbc.createDatabase();
	    //fill the db
	    fillDatabase();
  }
  
  public static void resetDB() {
	    //empty the db
	    DBCreator dbc = new DBCreator();
	    dbc.deleteDatabase();
	    dbc.createDatabase();
}
public static void main( String args[]) {
    resetDBToDefault();
  }
}
